package com.qzc.datafactory;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * 描述:
 * cms工厂
 *
 * @outhor 逆鳞雪狮
 * @create 2019-08-01 10:32
 */
@Configuration
@MapperScan(value = "com.qzc.targetmapper",sqlSessionFactoryRef = "sqlSessionTargetFactory")
public class DataTargetFactory {

    // 配置数据源
    @Resource(name = "dataSourceTarget")
    private DataSource dataSourceTarget;




    @Bean
    @Primary
    public SqlSessionFactory sqlSessionTargetFactory() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:targetmapper/*.xml"));
        factoryBean.setDataSource(dataSourceTarget); //  使用数据源

        return factoryBean.getObject();

    }

    @Bean
    @Primary
    public SqlSessionTemplate sqlSessionTemplateTarget() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate( sqlSessionTargetFactory()); // 使用上面配置的Factory
        return template;
    }
}
